// pages/shoppingTrolley/shoppingTrolley.js
const util = require("../../utils/util")
const app = getApp()
Page({

  data: {
    shoppingTrolley: null,
    productList: [],
    shoppingTrolleyHasProduct: false,
    temperature: ["正常冰", "少冰", "去冰", "常温", "温热", "烫热"],
    //extra Hot=2; Hot=1; normal0; no ice=-1; less ice=-2; iced=-3 
    sweetness: ["无糖", "微糖", "半糖", "少糖", "全糖"]
    //no sugar=0; 25% sugar=1; 50% sugar=2; 75% sugar=3; 100% sugar=4
  },

  buythese() {
    //买下购物车里所有产品 将购物车订单号传回后台 将开始时间从null改为现在时刻 标记已买
    var that = this
    wx.request({
      url: util.BASE_URL + '/order/buythese',
      method: 'POST',
      data: app.globalData.orderList,
      success(res) {
        console.log('成功提交！')
        app.globalData.shoppingTrolley = null
        app.globalData.shoppingTrolleyProductIndex = -1
        app.globalData.orderList = null
        that.setData({
          shoppingTrolleyHasProduct: false,
          shoppingTrolley: null
        })
      },
      fail(err){
        console.log(err)
      }
    })
  },

  choseOne(e) {
    // 打算继续调用product页面进行用户修改奶茶
    app.globalData.productIndex = e.currentTarget.dataset.productindex
    //将用户在购物车所选择的哪个产品，哪个小定单写入全局变量
    wx.navigateTo({
      url: '/pages/product/product',
    })
  },

  onShow: function () {
    var that = this;
    that.setData({
      productList: app.globalData.productList
    })
    if (app.globalData.orderList != null) {
      //若存在订单，通过订单号获取用户已经加购的物品
      wx.request({
        url: util.BASE_URL + "/get/shoppingtrolley",
        method: "POST",
        data: app.globalData.orderList,
        success(res) { //得到OrderedProduct列表
          app.globalData.shoppingTrolley = res.data
          that.setData({
            shoppingTrolley: app.globalData.shoppingTrolley,
            shoppingTrolleyHasProduct: true
          })
          console.log('购物车页面的JS的shoppingTrolley：', that.data.shoppingTrolley)
          console.log('全局变量的产品信息', that.data.productList)
        }
        //不成功shoppingTrolleyHasProduct变量默认为false
      })
    }
    //没有订单号shoppingTrolleyHasProduct变量默认为false

  },


})